/**
 * CreateCircleActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-13 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jamin.aabill.R;
import com.jamin.aabill.custom.TopBar;
import com.jamin.aabill.custom.TopBarClickListener;
import com.jamin.aabill.dao.CircleDao;
import com.jamin.aabill.data.Circle;
import com.umeng.analytics.MobclickAgent;

/**
 * ClassName:CreateCircleActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-13 上午11:13:29 
 */
public class CreateCircleActivity extends Activity implements OnClickListener{

	private static final String TAG = "CreateCircleActivity";
	//	private static final int DIALOG_CREATE_CIRCLE = 0;
	private static final int MSG_CREATE_OK = 0;
	//	private static final int MSG_CREATE_FAIL = 1;
	private long circleId = -1;
	//TopBar
	TopBar topBar = null;
	//Btn
	Button mCommitBtn = null;
	//EditText
	EditText mNameEditText = null;
	EditText mDescriptionEditText = null;

	//创建账单
	Thread mThread= null;
	ProgressDialog mCreateCircleDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.create_circle_activity);
		circleId = getIntent().getLongExtra("circleId", -1);
		initView();
		initData();
	}


	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}


	private void initView() {
		//TopBar
		topBar = (TopBar) findViewById(R.id.top_bar);
		topBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
			}
			@Override
			public void right2BtnClick() {
			}
			@Override
			public void leftBtnClick() {
				CreateCircleActivity.this.finish();
			}
		});

		mCommitBtn = (Button) findViewById(R.id.create_circle_ok);
		mNameEditText = (EditText) findViewById(R.id.create_circle_name);
		mDescriptionEditText = (EditText) findViewById(R.id.create_circle_description);
		mCommitBtn.setOnClickListener(this);

	}

	private void initData() {
		if(circleId != -1){
			CircleDao mCircleDao = new CircleDao();
			Circle circle = mCircleDao.getCircle(circleId);
			mNameEditText.setText(circle.getCircle_name());
			mDescriptionEditText.setText(circle.getCircle_description());
			topBar.setTitle(getString(R.string.create_circle_activity_title_modify));
		}

	}





	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.create_circle_ok:
			commit();
			break;

		}
	}

	/**
	 * check name field , shake the NameEditText if field null 
	 * @method commit
	 * @throws 
	 * @since v1.0
	 */
	private void commit() {
		if(!TextUtils.isEmpty(mNameEditText.getText().toString())){
			if(mThread != null){
				mThread = null;
			}
			mThread = new Thread(mCreateCircleRunnable);
			mThread.start();
		}else{
			Animation shake = AnimationUtils.loadAnimation(CreateCircleActivity.this, R.anim.shake);
			mNameEditText.setAnimation(shake);
		}

	}


	Runnable mCreateCircleRunnable = new Runnable() {
		@Override
		public void run() {
			Circle circle = new Circle(circleId , mNameEditText.getText().toString() , mDescriptionEditText.getText().toString());
			CircleDao mDao = new CircleDao();
			mDao.replease(circle);
			finish();
		}
	};


	/**
	 * 消息处理<br>
	 */
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case MSG_CREATE_OK:
				Toast.makeText(CreateCircleActivity.this, getString(R.string.create_circle_activity_create_ok), Toast.LENGTH_SHORT).show();
				finish();
				break;
			default:
				break;
			}
		}

	};

}

